{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "908b0770",
   "metadata": {},
   "source": [
    "-------\n",
    "* 日期：2022-10-19 \\ week09（周三）\n",
    "* 课程：python\n",
    "* 记录人：huangbairui\n",
    "-------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2db9f1e0",
   "metadata": {},
   "source": [
    "# 本周的学习目标\n",
    "\n",
    "* 1.列表的方法\n",
    "\n",
    "* 2.whli"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0b12fa0",
   "metadata": {},
   "source": [
    "# 1.列表的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d527d917",
   "metadata": {},
   "source": [
    "1.使用tab键查看列表的方法\n",
    "\n",
    "2.使用help（查看方法的使用规则）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a13d9166",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jack', 'Alex', 'Marry', 'Jan']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"jack\",\"Alex\",\"Marry\",\"Jan\"]\n",
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c96423d",
   "metadata": {},
   "source": [
    "# 1.1列表的增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1332d3bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"Lihua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9ac5e512",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jack', 'Alex', 'Marry', 'Jan', 'Lihua']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7df6787d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bb96577d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "cdd869ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jack', 'Alex', 'Marry', 'Jan', 'Lihua']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f5a1d92d",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.insert(2,\"Zhchao\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8a4baf8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jack', 'Alex', 'Zhchao', 'Marry', 'Jan', 'Lihua']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77b0e84e",
   "metadata": {},
   "source": [
    "# 2.列表的删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "77e23584",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "63a4e29d",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [19]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mnames\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mZhichao\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "names.remove(\"Zhichao\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7e7e4a80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jack', 'Alex', 'Zhchao', 'Marry', 'Jan', 'Lihua']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37f82351",
   "metadata": {},
   "source": [
    "* 正确的删除方法的流程"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0a64832",
   "metadata": {},
   "source": [
    "1.查询value是否存在列表当中\n",
    "\n",
    "2.对存在的value进行删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e1d25448",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Wang\" in names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "102f60d3",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [22]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mnames\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mWang\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "names.remove(\"Wang\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "42f73249",
   "metadata": {},
   "outputs": [],
   "source": [
    "if \"Wang\" in names:\n",
    "    names.remove(\"Wang\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "5980cb61",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：删除系统：\n",
    "## 1.如果输入的值存在，删除2.如果输入的值不存在，提示用户查询无结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "65be63ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jack', 'Alex', 'Zhchao', 'Marry', 'Jan', 'Lihua']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "7c1ac0eb",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "Interrupted by user",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Input \u001b[1;32mIn [35]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m user_input_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43minput\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m请输入要删除的内容: \u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m user_input_name \u001b[38;5;129;01min\u001b[39;00m names:\n\u001b[0;32m      3\u001b[0m     names\u001b[38;5;241m.\u001b[39mremove(user_input_name)\n",
      "File \u001b[1;32mC:\\Anaconda\\lib\\site-packages\\ipykernel\\kernelbase.py:1075\u001b[0m, in \u001b[0;36mKernel.raw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m   1071\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_allow_stdin:\n\u001b[0;32m   1072\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m StdinNotImplementedError(\n\u001b[0;32m   1073\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m   1074\u001b[0m     )\n\u001b[1;32m-> 1075\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_input_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m   1076\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mprompt\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1077\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_parent_ident\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mshell\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1078\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_parent\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mshell\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1079\u001b[0m \u001b[43m    \u001b[49m\u001b[43mpassword\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m   1080\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mC:\\Anaconda\\lib\\site-packages\\ipykernel\\kernelbase.py:1120\u001b[0m, in \u001b[0;36mKernel._input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m   1117\u001b[0m             \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[0;32m   1118\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m:\n\u001b[0;32m   1119\u001b[0m     \u001b[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[1;32m-> 1120\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInterrupted by user\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28mNone\u001b[39m\n\u001b[0;32m   1121\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m   1122\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mlog\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid Message:\u001b[39m\u001b[38;5;124m\"\u001b[39m, exc_info\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
     ]
    }
   ],
   "source": [
    "user_input_name = input(\"请输入要删除的内容: \")\n",
    "if user_input_name in names:\n",
    "    names.remove(user_input_name)\n",
    "else:\n",
    "    print(\"您输入的结果不存在, 删除无效\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8346f04a",
   "metadata": {},
   "source": [
    "------\n",
    "* 列表的删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "edd4d36a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 重新建立names的列表数据\n",
    "name = ['jack', 'Alex',  'Marry', 'Jan', 'Lihua']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44a8d1b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b583de9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(names.removo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ceea535",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(names.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45f2d6b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.pop() # 直接使用pop（），删除最后一位。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83d694a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e458ac76",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e92a33f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e574d53e",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.pop(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "903cce4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jack', 'Alex', 'Zhchao', 'Marry', 'Jan', 'Lihua']"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "af4af6d9",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41ca5817",
   "metadata": {},
   "outputs": [],
   "source": [
    "pop_index = int(input(\"输入删除的索引值: \"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5f91667",
   "metadata": {},
   "outputs": [],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c976ed33",
   "metadata": {},
   "source": [
    "# 3.列表的修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17607c43",
   "metadata": {},
   "outputs": [],
   "source": [
    "names[0] = \"Zhichao\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef20fa33",
   "metadata": {},
   "outputs": [],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f20172bf",
   "metadata": {},
   "source": [
    "# 4.列表的其他方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "849b7af8",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = ['jack', 'Alex', 'Zhchao', 'Marry', 'Jan', 'Lihua']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a6481b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b04a00a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42a1fb49",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(names.exend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d270430",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
